package com.member.shop.service.impl;

import com.member.shop.domain.*;
import com.member.shop.mapper.SysBizUserIntegralRecordMapper;
import com.member.shop.service.*;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizIntegralConfig;
import com.member.shop.service.ISysBizIntegralConfigService;
import com.member.shop.domain.SysBizGradeInfo;
import com.member.shop.service.ISysBizGradeInfoService;
import com.member.system.cache.CommonCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 用户积分记录Service业务层处理
 *
 * @author thomas
 * @date 2024-10-27
 */
@Service
@Slf4j
public class SysBizUserIntegralRecordServiceImpl implements ISysBizUserIntegralRecordService {
    @Autowired
    private SysBizUserIntegralRecordMapper sysBizUserIntegralRecordMapper;


    @Autowired
    private ISysBizIntegralConfigService integralConfigService;

    @Resource
    private ISysBizUserService sysBizUserService;

    @Override

    public Integer addIntegralRecord(SysBizUser sysBizUser, String typeCode) {

        try {
            return addUserIntegralRecord(sysBizUser, typeCode);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("增加积分异常：", e);
        }
        return 0;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addIntegralRecordByText(SysBizUser sysBizUser, String typeCode, Integer integral) {
        SysBizUserIntegralRecord sysBizUserIntegralRecord = new SysBizUserIntegralRecord();
        sysBizUserIntegralRecord.setUserId(sysBizUser.getId());
        sysBizUserIntegralRecord.setUserName(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setBehavior(typeCode);
        sysBizUserIntegralRecord.setPhone(sysBizUser.getUserPhone());
        sysBizUserIntegralRecord.setScore(integral);
        sysBizUserIntegralRecord.setChangeType(1);
        sysBizUserIntegralRecord.setChangeDate(LocalDate.now());

        sysBizUserIntegralRecord.setBussType(Integer.parseInt(typeCode));
        // sysBizUserService.updateUserScore(sysBizUser.getUid(), integral);

        sysBizUserIntegralRecord.setCreateBy(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setCreateTime(new Date());
        sysBizUserIntegralRecord.setUpdateBy(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setUpdateTime(new Date());
        this.insertSysBizUserIntegralRecord(sysBizUserIntegralRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer addUserIntegralRecord(SysBizUser sysBizUser, String typeCode) {


        SysBizUserIntegralRecord sysBizUserIntegralRecord = new SysBizUserIntegralRecord();
        SysBizIntegralConfig sysBizIntegralConfig = integralConfigService.selectSysBizIntegralConfigByTypeCode(typeCode, sysBizUser.getTenantId());


        sysBizUserIntegralRecord.setTenantId(sysBizUser.getTenantId());

        String behavior = sysBizIntegralConfig.getRemark();

        String dictDataValue = CommonCache.getDictDataValue("biz_integral_type" + typeCode);
        if (StringUtils.isNotEmpty(dictDataValue)) {
            sysBizUserIntegralRecord.setBehavior(dictDataValue);
        }
        Integer integral = sysBizIntegralConfig.getIntegral();
        Integer period = sysBizIntegralConfig.getPeriod();
        if (period != null && period != 0) {
            List<SysBizUserIntegralRecord> sysBizUserIntegralRecords = null;
            if (period == 1) {
                SysBizUserIntegralRecord userIntegralRecord = new SysBizUserIntegralRecord();
                userIntegralRecord.setUserId(sysBizUser.getId());
                userIntegralRecord.setChangeDate(LocalDate.now());
                userIntegralRecord.setBehavior(behavior);
                sysBizUserIntegralRecords = this.selectSysBizUserIntegralRecordList(userIntegralRecord);


            } else if (period == 2) {
                SysBizUserIntegralRecord userIntegralRecord = new SysBizUserIntegralRecord();
                userIntegralRecord.setUserId(sysBizUser.getId());
                userIntegralRecord.setBehavior(behavior);
                sysBizUserIntegralRecords = this.selectSysBizUserIntegralRecordList(userIntegralRecord);

            }

            if (!CollectionUtils.isEmpty(sysBizUserIntegralRecords) && sysBizUserIntegralRecords.size() > sysBizIntegralConfig.getUpperLimit()) {
                log.info("user:" + sysBizUser.getId() + "," + behavior + ",已达到获取上限," + sysBizUserIntegralRecords.size());
                return 0;
            }
        }

        sysBizUserIntegralRecord.setUserId(sysBizUser.getId());
        sysBizUserIntegralRecord.setUserName(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setBehavior(behavior);
        sysBizUserIntegralRecord.setPhone(sysBizUser.getUserPhone());
        sysBizUserIntegralRecord.setScore(integral);
        sysBizUserIntegralRecord.setChangeType(1);
        sysBizUserIntegralRecord.setChangeDate(LocalDate.now());
        sysBizUserIntegralRecord.setBussType(Integer.parseInt(typeCode));
//        sysBizUserService.updateUserScore(sysBizUser.getUid(), integral);

        sysBizUserIntegralRecord.setCreateBy(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setCreateTime(new Date());
        sysBizUserIntegralRecord.setUpdateBy(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setUpdateTime(new Date());

        //等级
        SysBizUser user = sysBizUserService.selectSysBizUserById(sysBizUser.getId());
        Long score = user.getScore();

        Long startScore = score - integral;
        List<SysBizGradeInfo> sysBizGradeInfos = gradeInfoService.selectSysBizGradeInfoListByIntegral(0L, score);

        SysBizGradeInfo currentGrade = null;

        this.insertSysBizUserIntegralRecord(sysBizUserIntegralRecord);

        return integral;
    }


    @Autowired
    private ISysBizGradeInfoService gradeInfoService;


    @Override
    public void minusIntegralRecord(SysBizUser sysBizUser, String typeCode, Integer integral) {

        try {
            minusUserIntegralRecord(sysBizUser, typeCode, integral);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("减少积分异常：", e);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void minusUserIntegralRecord(SysBizUser sysBizUser, String typeCode, Integer integral) {
        SysBizUserIntegralRecord sysBizUserIntegralRecord = new SysBizUserIntegralRecord();
        sysBizUserIntegralRecord.setUserId(sysBizUser.getId());
        sysBizUserIntegralRecord.setUserName(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setBehavior("");
        sysBizUserIntegralRecord.setPhone(sysBizUser.getUserPhone());
        sysBizUserIntegralRecord.setScore(integral);
        sysBizUserIntegralRecord.setChangeType(2);
        sysBizUserIntegralRecord.setChangeDate(LocalDate.now());

        if ("4".equals(typeCode)) {
            sysBizUserIntegralRecord.setRemark("积分抵扣");
        }

        String dictDataValue = CommonCache.getDictDataValue("biz_deduction_behavior" + typeCode);
        if (StringUtils.isNotEmpty(dictDataValue)) {
            sysBizUserIntegralRecord.setBehavior(dictDataValue);
        }

        sysBizUserIntegralRecord.setBussType(Integer.parseInt(typeCode));
//        sysBizUserService.updateUserUseScore(sysBizUser.getUid(), integral, "");

        sysBizUserIntegralRecord.setCreateBy(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setCreateTime(new Date());
        sysBizUserIntegralRecord.setUpdateBy(sysBizUser.getNickName());
        sysBizUserIntegralRecord.setUpdateTime(new Date());


        this.insertSysBizUserIntegralRecord(sysBizUserIntegralRecord);
    }

    @Override
    public void unIntegralRecord(SysBizUser sysBizUser, Integer integral) {


        this.minusIntegralRecord(sysBizUser, "3", integral);
    }

    /**
     * 查询用户积分记录
     *
     * @param id 用户积分记录主键
     * @return 用户积分记录
     */
    @Override
    public SysBizUserIntegralRecord selectSysBizUserIntegralRecordById(Long id) {
        return sysBizUserIntegralRecordMapper.selectSysBizUserIntegralRecordById(id);
    }

    /**
     * 查询用户积分记录列表
     *
     * @param sysBizUserIntegralRecord 用户积分记录
     * @return 用户积分记录
     */
    @Override
    public List<SysBizUserIntegralRecord> selectSysBizUserIntegralRecordList(SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        return sysBizUserIntegralRecordMapper.selectSysBizUserIntegralRecordList(sysBizUserIntegralRecord);
    }

    @Override
    public List<SysBizUserIntegralRecord> selectSysBizUserIntegralRecordListByBussType(Integer bussType) {
        SysBizUserIntegralRecord sysBizUserIntegralRecord = new SysBizUserIntegralRecord();
        sysBizUserIntegralRecord.setBussType(bussType);
        return sysBizUserIntegralRecordMapper.selectSysBizUserIntegralRecordList(sysBizUserIntegralRecord);
    }

    /**
     * 新增用户积分记录
     *
     * @param sysBizUserIntegralRecord 用户积分记录
     * @return 结果
     */
    @Override
    public int insertSysBizUserIntegralRecord(SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        sysBizUserIntegralRecord.setCreateTime(DateUtils.getNowDate());
        return sysBizUserIntegralRecordMapper.insertSysBizUserIntegralRecord(sysBizUserIntegralRecord);
    }

    /**
     * 修改用户积分记录
     *
     * @param sysBizUserIntegralRecord 用户积分记录
     * @return 结果
     */
    @Override
    public int updateSysBizUserIntegralRecord(SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        sysBizUserIntegralRecord.setUpdateTime(DateUtils.getNowDate());
        return sysBizUserIntegralRecordMapper.updateSysBizUserIntegralRecord(sysBizUserIntegralRecord);
    }

    /**
     * 批量删除用户积分记录
     *
     * @param ids 需要删除的用户积分记录主键
     * @return 结果
     */
    @Override
    public int deleteSysBizUserIntegralRecordByIds(Long[] ids) {
        return sysBizUserIntegralRecordMapper.deleteSysBizUserIntegralRecordByIds(ids);
    }

    /**
     * 删除用户积分记录信息
     *
     * @param id 用户积分记录主键
     * @return 结果
     */
    @Override
    public int deleteSysBizUserIntegralRecordById(Long id) {
        return sysBizUserIntegralRecordMapper.deleteSysBizUserIntegralRecordById(id);
    }

    @Override
    public int deleteSysBizUserIntegralRecordByBussKey(SysBizUserIntegralRecord sysBizUserIntegralRecord) {
        return sysBizUserIntegralRecordMapper.deleteSysBizUserIntegralRecordByBussKey(sysBizUserIntegralRecord);
    }
}
